/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * 
 * @author dingzhentao
 * @since 2015年9月7日
 * @version 0.1
 */
package com.suredy.app.testType.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.Constants;
import com.suredy.app.testItem.model.TestItem;
import com.suredy.app.testItem.service.TestItemSrv;
import com.suredy.app.testType.model.TestType;
import com.suredy.app.testType.srv.TestTypeSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;

/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value = "/app")
public class TestTypeCtrl extends BaseCtrl{

	private final static Logger log = LoggerFactory.getLogger(TestTypeCtrl.class);

	@Autowired
	private TestTypeSrv service;
	
	@Autowired
	private TestItemSrv testItemsrv;

	@RequestMapping(value = "testType/list")
	public ModelAndView list(String page, String size, String name) {
		ModelAndView view = new ModelAndView("/app/testType/list");
		int pageIndex = 1, pageSize = Constants.DEFAULT_PAGE_SIZE;
		if (!StringUtils.isBlank(page)) {
			pageIndex = Integer.parseInt(page);
		}
		if (!StringUtils.isBlank(size)) {
			pageSize = Integer.parseInt(size);
		}
		List<TestType> data = null;
		Long count;
		if (StringUtils.isBlank(name)) {
			count = this.service.Count();
			data = this.service.getAll(pageIndex, pageSize);
		} else {
			data = this.service.getByName(name);
			count = data == null ? 0L : (long) data.size();
		}
		view.addObject("data", data);
		view.addObject("pageSize", pageSize);
		view.addObject("pageIndex", pageIndex);
		view.addObject("count", count);
		return view;
	}
	
	@RequestMapping("testType/get")
	@ResponseBody
	public Object get(String itemId) {
		if (StringUtils.isBlank(itemId) ) {
			log.info("Parameter String[itemId] is blank.");
			return MessageModel.createErrorMessage("参数不足: 区域ID必须提供", null);
		}
		TestType item = this.service.getById(itemId);
		return MessageModel.createSuccessMessage(null, item);
	}

	@RequestMapping(value = "testType/create")
	public ModelAndView create() {
		List<TestItem> l = testItemsrv.getAll();
		ModelAndView view = new ModelAndView("/app/testType/create");
		view.addObject("testItems", l);
		return view;
	}

	@RequestMapping(value = "testType/create-save")
	@ResponseBody
	public Object doCreate(HttpServletRequest request) {
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String description = request.getParameter("description");;
		if (StringUtils.isBlank(name)) {
			log.info("Parameter String[name] is blank.");
			return MessageModel.createErrorMessage("参数不足: 区域名称必须提供", null);
		}
		TestType item = new TestType();
		item.setCode(StringUtils.isBlank(code) ? null : code);
		item.setDescription(StringUtils.isBlank(description) ? null : description);
		item.setName(name);
		this.service.save(item);
		return MessageModel.createSuccessMessage(null, item.getId());

	}

	@RequestMapping(value = "testType/edit")
	public ModelAndView eidt(String itemId) {
		List<TestItem> l = testItemsrv.getAll();
		ModelAndView view = new ModelAndView("/app/testType/edit");
		if (StringUtils.isBlank(itemId)) {
			log.info("Parameter String[attributionId] is blank.");
			return view;
		}
		TestType item = this.service.getById(itemId);
		view.addObject("testItems", l);
		view.addObject("item", item);
		return view;
	}

	@RequestMapping(value = "testType/edit-save")
	@ResponseBody
	public Object doEdit(HttpServletRequest request) {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String description = request.getParameter("description");;
		if (StringUtils.isBlank(name) || StringUtils.isBlank(id)) {
			log.info("Parameter String[name|ID] is blank.");
			return MessageModel.createErrorMessage("参数不足: 名称|ID必须提供", null);
		}
		TestType item = this.service.get(id);
		if (item == null) {
			log.info("Can't found the information which ID is '" + id + "'");
			return MessageModel.createErrorMessage("未找到与ID对应的信息", id);
		} else {
			item.setCode(StringUtils.isBlank(code) ? null : code);
			item.setDescription(StringUtils.isBlank(description) ? null : description);
			item.setName(name);
			this.service.update(item);
			return MessageModel.createSuccessMessage(null, null);
		}
	}

	@RequestMapping(value = "testType/delete")
	@ResponseBody
	public Object remove(String itemId) {
		if (StringUtils.isBlank(itemId)) {
			log.info("Parameter String[itemId] is blank.");
			return MessageModel.createErrorMessage("参数不足:ID必须提供", null);
		}
		TestType entity = this.service.get(itemId);
		if (entity == null) {
			log.info("Can't found the information which ID is '" + itemId + "'");
			return MessageModel.createErrorMessage("未找到与ID对应的信息", itemId);
		} else {
			this.service.delete(entity);
			return MessageModel.createSuccessMessage(null, null);
		}
	}
	
}
